package com.dropTables.TakingNote;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.KeyEvent;


public class TextEditActivity extends Activity {
	public static EditText rTitleText;
	public static EditText rBodyText;
	private Long rRowId;
	@Override
	protected void onStart()
	{
		super.onStart();
	}
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.normal_edit);
		rTitleText = (EditText) findViewById(R.id.title);
		
		rBodyText = (EditText) findViewById(R.id.body);
		
		Button confirm_button = (Button) findViewById(R.id.confirm_normal);
		
		//Check to see if you are editing the reminder, if so set all the fields properly.
		Bundle extras = getIntent().getExtras();
		if (extras != null) 
		{
			rRowId = extras.getLong(ReminderDbHelper.KEY_ROWID);
			rRowId = NoteEditActivity.rRowId;
			if (NoteEditActivity.title != null) 
			{
				rTitleText.setText(extras.getString(ReminderDbHelper.KEY_TITLE));
			}
			if (NoteEditActivity.body != null) 
			{
				rBodyText.setText(extras.getString(ReminderDbHelper.KEY_BODY));
			}
			
		}
		confirm_button.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View view) 
			{
				//Get the values of the Text fields.
				String title = rTitleText.getText().toString();
				String body = rBodyText.getText().toString();
				String search = NoteEditActivity.gen_loc;
				String spec_loc = NoteEditActivity.spec_loc;
				
				
				
				//if user doesn't set title and body, put a default
				if (title.equals(""))
				{
					title = "Title";
				}
				if (body.equals(""))
				{
					body = "body";
				}
				//Get the date-time formatted string.
				Bundle bundle = new Bundle();
				bundle.putString(ReminderDbHelper.KEY_TITLE, title);
				bundle.putString(ReminderDbHelper.KEY_BODY, body);
				bundle.putString(ReminderDbHelper.KEY_G_LOC, search);
				bundle.putString(ReminderDbHelper.KEY_S_LOC, spec_loc);
				String tStamp = "";
				if(NoteEditActivity.hour>0)
				{
					int hour=NoteEditActivity.hour;
					System.out.println(hour);
					int min =NoteEditActivity.min;
					int day=NoteEditActivity.day;
					int month =NoteEditActivity.month;
					int year = NoteEditActivity.year;
					tStamp = String.format("%04d",year)+"-"+String.format("%02d",month+1)+"-"+String.format("%02d",day)+" "+String.format("%02d",hour)+":"+String.format("%02d",min)+":00";

				}
				bundle.putString(ReminderDbHelper.KEY_TIME, tStamp);
				if (NoteEditActivity.rRowId != -1) 
				{
					bundle.putLong(ReminderDbHelper.KEY_ROWID, NoteEditActivity.rRowId);
					bundle.putLong(ReminderDbHelper.KEY_R_ID,NoteEditActivity.rId);
				}
				Intent rIntent = new Intent();
				rIntent.putExtras(bundle);
				getParent().setResult(RESULT_OK, rIntent);
				finish();
			}
		});
		
	}
}